package topin.facecat.FaceCat.UI

import facecat.topin.core.FCColor
import facecat.topin.core.FCFont
import facecat.topin.core.FCHorizontalAlign
import facecat.topin.core.FCPaint
import facecat.topin.core.FCPoint
import facecat.topin.core.FCRect
import facecat.topin.grid.FCGridDoubleCell
import facecat.topin.grid.FCGridSelectionMode

class FCGridDoubleCellEx : FCGridDoubleCell() {
    var m_underLine = false

    init {
        digit = 2
    }

    override fun onPaint(paint: FCPaint, rect: FCRect, clipRect: FCRect, isAlternate: Boolean) {
        val clipW = clipRect.right - clipRect.left
        val clipH = clipRect.bottom - clipRect.top
        if (clipW > 0 && clipH > 0) {
            if (m_grid != null && m_row != null && m_column != null) {
                //判断选中
                val text = paintText
                var selected = false
                if (m_grid.selectionMode == FCGridSelectionMode.SelectCell) {
                    val selectedCells = m_grid.selectedCells
                    val selectedCellSize = selectedCells.size
                    for (i in 0 until selectedCellSize) {
                        if (selectedCells[i] === this) {
                            selected = true
                            break
                        }
                    }
                } else if (m_grid.selectionMode == FCGridSelectionMode.SelectFullColumn) {
                    val selectedColumns = m_grid.selectedColumns
                    val selectedColumnsSize = selectedColumns.size
                    for (i in 0 until selectedColumnsSize) {
                        if (selectedColumns[i] === m_column) {
                            selected = true
                            break
                        }
                    }
                } else if (m_grid.selectionMode == FCGridSelectionMode.SelectFullRow) {
                    val selectedRows = m_grid.selectedRows
                    val selectedRowsSize = selectedRows.size
                    for (i in 0 until selectedRowsSize) {
                        if (selectedRows[i] === m_row) {
                            selected = true
                            break
                        }
                    }
                }

                //获取颜色
                var font: FCFont? = null
                var backColor = FCColor.None
                var textColor = FCColor.None
                var autoEllipsis = m_grid.autoEllipsis()
                var rowStyle = m_grid.rowStyle
                if (isAlternate) {
                    val alternateRowStyle = m_grid.alternateRowStyle
                    if (alternateRowStyle != null) {
                        rowStyle = alternateRowStyle
                    }
                }
                if (rowStyle != null) {
                    if (m_grid.selectionMode != FCGridSelectionMode.SelectFullRow) {
                        if (backColor == FCColor.None) {
                            backColor = if (selected) {
                                rowStyle.selectedBackColor
                            } else if (m_row === m_grid.hoveredRow) {
                                rowStyle.hoveredBackColor
                            } else {
                                rowStyle.backColor
                            }
                        }
                    }
                    if (font == null) {
                        font = rowStyle.font
                    }
                    if (textColor == FCColor.None) {
                        textColor = if (selected) {
                            rowStyle.selectedTextColor
                        } else if (m_row === m_grid.hoveredRow) {
                            rowStyle.hoveredTextColor
                        } else {
                            rowStyle.textColor
                        }
                    }
                }
                var horizontalAlign = m_column.cellAlign
                if (m_style != null) {
                    if (m_style.autoEllipsis()) {
                        autoEllipsis = m_style.autoEllipsis()
                    }
                    if (m_style.backColor != FCColor.None) {
                        backColor = m_style.backColor
                    }
                    if (m_style.font != null) {
                        font = m_style.font
                    }
                    if (m_style.textColor != FCColor.None) {
                        textColor = m_style.textColor
                    }
                    if (m_style.align != FCHorizontalAlign.Inherit) {
                        horizontalAlign = m_style.align
                    }
                }
                rect.bottom -= 1
                paint.fillRect(backColor, rect)
                val tSize = paint.textSize(text, font, -1)
                val tPoint = FCPoint(rect.left + 1, rect.top + clipH / 2 - tSize.cy / 2)
                val width = rect.right - rect.left
                if (tSize.cx < width) {
                    if (horizontalAlign == FCHorizontalAlign.Center) {
                        tPoint.x = rect.left + (rect.right - rect.left - tSize.cx) / 2
                    } else if (horizontalAlign == FCHorizontalAlign.Right) {
                        tPoint.x = rect.right - tSize.cx - 2
                    }
                }
                val tRect = FCRect(tPoint.x, tPoint.y, tPoint.x + tSize.cx, tPoint.y + tSize.cy)
                if (autoEllipsis && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom)) {
                    if (tRect.right > clipRect.right) {
                        tRect.right = clipRect.right
                    }
                    if (tRect.bottom > clipRect.bottom) {
                        tRect.bottom = clipRect.bottom
                    }
                    paint.drawTextAutoEllipsis(text, textColor, font, tRect)
                } else {
                    paint.drawText(text, textColor, font, tRect, -1)
                }
            }
        }
    }
}

